/*!
 * @fileOverview Utility functions.
 */

(function(exports, $, _) {

	"use strict";

	/**
	 * Check Smartphone dicision.
	 */
	$.smartDecision = (function() {
		var ua = window.navigator.userAgent.toLowerCase();

		return {
			Tablet: (ua.indexOf("windows") !== -1 && ua.indexOf("touch") !== -1) || ua.indexOf("ipad") !== -1 || (ua.indexOf("android") !== -1 && ua.indexOf("mobile") === -1) || (ua.indexOf("firefox") !== -1 && ua.indexOf("tablet") !== -1) || ua.indexOf("kindle") !== -1 || ua.indexOf("silk") !== -1 || ua.indexOf("playbook") !== -1,
			Mobile: (ua.indexOf("windows") !== -1 && ua.indexOf("phone") !== -1) || ua.indexOf("iphone") !== -1 || ua.indexOf("ipod") !== -1 || (ua.indexOf("android") !== -1 && ua.indexOf("mobile") !== -1) || (ua.indexOf("firefox") !== -1 && ua.indexOf("mobile") !== -1) || ua.indexOf("blackberry") !== -1
		};
	})();

	



	var Modal = function(el, options) {
		return (this instanceof Modal) ? this.initialize(el, options) : new Modal(el, options);
	};

	Modal.prototype.initialize = function(el, options) {
		var self = this;

		var opts = $.extend({
			speed: 500,
			width: 'auto',
			height: 'auto'
		}, options);

		self.$body = $('body');

		self.$el = $(el);
		self.$overlay = $('#js-modalOverlay');
		self.$modal = $('#js-modal');
		self.$header = $('#js-header');
		self.$footer = $('#js-footer');

		self.fadeSpeed = opts.speed;
		self.isIframe = opts.iframe;
		self.isInline = opts.inline;
		self.modalWidth = parseInt(opts.width);
		self.modalHeight = parseInt(opts.height);

		self.setTriggerEvent();
		self.setOverlayEvent();
		$(window).on('resize', _.throttle(function() {
			self.onResize(self);
		}, 500));
	};

	Modal.prototype.setTriggerEvent = function() {
		var self = this;

		self.$el.on('click', function(event) {
			if ($.smartDecision.Mobile || $.smartDecision.Tablet) {
				return;
			}

			event.preventDefault();

			self.showModal();
			self.prepModal();
		});
	};

	Modal.prototype.setOverlayEvent = function() {
		var self = this;

		self.$overlay.on('click', function() {
			self.hideModal();
		});
	};

	Modal.prototype.showModal = function() {
		var self = this;

		self.$overlay
			.css({
				visibility: 'visible',
				display: 'block'
			})
			.stop(true, true)
			.transition({
				opacity: 0.8
			}, self.fadeSpeed);

		self.$modal
			.css({
				visibility: 'visible',
				display: 'block'
			})
			.stop(true, true)
			.transition({
				opacity: 1
			}, self.fadeSpeed);

		$(document).on('keydown.escape', function(event) {
			if (event.which === 27) {
				self.hideModal();
			}
		});
	};

	Modal.prototype.prepModal = function() {
		var self = this;

		self.href = self.$el.attr('href');
		self.$html = $('<iframe src=' + self.href + ' frameborder="0" onmousewheel=""></iframe>');
		self.$modal.html(self.$html);
	};

	Modal.prototype.hideModal = function() {
		var self = this;

		$(document).off('keydown.escape');

		self.$overlay
			.stop(true, true)
			.transition({
				opacity: 0
			}, self.fadeSpeed, function() {
				self.isShow = false;

				self.$overlay
					.css({
						visibility: 'hidden',
						display: 'none'
					});
			});

		self.$modal
			.stop(true, true)
			.transition({
				opacity: 0
			}, self.fadeSpeed, function() {

				self.$modal.css({
					visibility: 'hidden',
					display: 'none'
				});
			});
	};

	Modal.prototype.onResize = function(self) {
		var winH = $(window).height(),
			winW = $(window).width(),
			modalHeight = winH - 100,
			modalWidth = self.$modal.width(),
			modalTop = Math.floor((winH - modalHeight) / 2),
			modalLeft = Math.floor((winW - modalWidth) / 2);

		self.$modal.css({
			top: modalTop,
			left: modalLeft,
			height: modalHeight
		});
	};

}(
	window,
	window.jQuery,
	window._
));

/**
 * @fileOverview Common functions.
 */

(function(exports, $, _) {

	"use strict";

	var Public = {
		winW: $(window).width(),
		winH: $(window).height(),
		gNav: null,
		isotope: null,
		elementFadeIn: null,
		isParent: window === window.parent
	};

	/**
	 * @module Common.
	 * @type {Object}
	 */
	var Common = {
		/**
		 * Initialize.
		 */
		initialize: function() {
			if (Public.isParent) {
				$('body').css('padding', 0);
				$('#js-header').css('display', 'block');
				$('#js-footer').css('display', 'block');
			} else {
				$('#js-content').css('padding', 0);
			}
		},

		/**
		 * Render.
		 * @return {Object}
		 */
		render: function() {

			//    this.setUpReturnTop();
			this.setUpLazyload();

			Public.isotope = this.setUpIsotope();
			Public.isotope.initialize();

			return this;
		},

		/**
		 * On load.
		 * @return {Object}
		 */
		onLoad: function() {

			Public.elementFadeIn = this.setUpElementFadeIn();
			Public.elementFadeIn.initialize();

			$(window).on('resize', _.throttle(Common.onResize, 50)).resize();

			return this;
		},

		/**
		 * Un load.
		 * @return {Object}
		 */
		unLoad: function() {
			$('html,body').scrollTop(0);
		},

		onResize: function() {
			Public.isotope.onIsotopeUpdate();

		},

		/**
		 * Set up isotope.
		 */
		setUpIsotope: function() {
			var $container = $('#js-portfolio'),
				$item = $container.find('.js-item'),
				$filter = $('#js-nav').find('.js-filter'),
				prevFilterValue = 'all',
				filterValue;
			var $grid = $('#js-portfolio').isotope({
				itemSelector: '.js-item',
			});

			var _onIsotopeUpdate = function() {
				$container.isotope({
					transitionDuration: 0
				});
			};

			var _setIsotope = function() {
				$container.isotope({
					itemSelector: '.js-item',
					columnWidth: '.js-gridSizer',
					percentPosition: true,
					isResizeBound: false
				});
			};
			var _onLoadedImage = function() {
				$container.isotope({
					filter: filterValue,
					transitionDuration: '0.8s'
				});

				Public.gNav.hide();
			};
			var _setUpDeferred = function($img) {
				var deferred = $.Deferred();

				$img.on('load', function() {
					deferred.resolve();
				});

				$img.on('error', function() {
					deferred.reject();
				});

				return deferred.promise();
			};
			var _setUpFilter = function() {
				// var hash = location.hash === '#all' ? '#js-item' : location.hash;

				// if(hash) {
				//   filterValue = hash.replace('#', '.');
				//   $(filterValue).find('img').trigger('unveil');
				//   prevFilterValue = filterValue;
				//   _onLoadedImage();
				// }

				// $filter.on('click', function(event) {
				//   var deferreds = [];

				//   event.preventDefault();

				//   filterValue = $(this).data('filter');

				//   if(prevFilterValue === filterValue) {
				//     return;
				//   }
				//   prevFilterValue = filterValue;

				//   $('html,body').scrollTop(0);

				//   if(filterValue === 'all') {
				//     filterValue = '.js-item';
				//     _onLoadedImage();
				//   } else {
				//     $(filterValue).each(function() {
				//       var $self = $(this)
				//         , $img = $self.find('img');

				//       if($self.hasClass('is-loadComp')) {
				//         return;
				//       }

				//       $img.trigger('unveil');
				//       deferreds.push(_setUpDeferred($img));
				//     });

				//     $.when
				//       .apply(null, deferreds)
				//       .always(function() {
				//         _onLoadedImage();
				//       });
				//     }
				// });
			};
			var getQueryString = function() {
				if (1 < document.location.search.length) {
					var query = document.location.search.substring(1);
					var parameters = query.split('&');

					var result = new Object();
					for (var i = 0; i < parameters.length; i++) {
						var element = parameters[i].split('=');

						var paramName = decodeURIComponent(element[0]);
						var paramValue = decodeURIComponent(element[1]);
						result[paramName] = decodeURIComponent(paramValue);
					}
					return result;
				}
				return null;
			};
			var filterChange = function(filterValue) {
				if (filterValue === ".cat-prof") {
					$("#js-portfolio").hide(500, function() {
						$(".profile  img.portfolio-itemImg").fadeTo(500, 1);
						$(".profile").show(500);
					});
					$("#js-content").addClass('active');
					setTimeout(function() {
						$("#js-content").removeClass('active');
					}, 1200);
				} else {
					$(".profile").hide(500);

					$("#js-portfolio").show(500, function() {
						$("#js-content").addClass('active');
						setTimeout(function() {
							$("#js-content").removeClass('active');
						}, 1200);
						$grid.isotope({
							filter: filterValue
						});
					});
				}
			};

			// load
			var hash = location.hash,
				$btns;
			if (!hash) {
				var get = getQueryString();
				if (get && 'js-hash' in get) {
					hash = '#' + get['js-hash'];
				}
			}
			if (hash) {
				hash = hash.replace('#!', '');
				switch (hash) {
					case 'cat-adv':
					case 'cat-edi':
					case 'cat-otr':
					case 'cat-prof':
						filterChange('.' + hash);
						$btns = $(".chd_nav_sub.button-group > button");
						$btns.removeClass('select');
						$btns.each(function() {
							if ($(this).data("filter") === '.' + hash) {
								$(this).addClass("select");
							}
						});
						break;
					default:
				}
			}
			// });

			var _initialize = function() {
				_setIsotope();
				_setUpFilter();
			};

			return {
				initialize: _initialize,
				onIsotopeUpdate: _onIsotopeUpdate
			};
		},

		/**
		 * Set up Element FadeIn.
		 */
		setUpElementFadeIn: function() {
			var $el = $('.js-fadeIn'),
				$win = $(window);

			var _onScroll = function(scrollTop) {
				var scrollBottom = scrollTop + $win.height();

				$el.each(function() {
					var $self = $(this),
						offsetTop = $self.offset().top;

					if ($self.hasClass('is-show')) {
						return;
					}

					if (scrollBottom > offsetTop) {
						$self
							.addClass('is-show')
							.delay(300)
							.transition({
								top: 0,
								opacity: 1
							}, 1500, 'easeOutCubic');
					}
				});
			};

			var _setUpFadeIn = function() {
				$el.css({
					position: 'relative',
					top: 40,
					opacity: 0
				});
			};

			var _initialize = function() {
				_onScroll(0);
			};

			return {
				initialize: _initialize,
				onScroll: _onScroll
			};
		},

		/**
		 * Set up lazy load.
		 */
		setUpLazyload: function() {
			var $portfolio = $('#js-portfolio'),
				$lazy = $portfolio.find('.js-lazy');

			$lazy.unveil(200, function() {
				$(this).on('load', function() {
					$(this).parents('.js-item').addClass('is-loadComp');
					Public.isotope.onIsotopeUpdate();
				});

				$(this).on('error', function() {
					$(this).parents('.js-item').remove();
				});
			});
		},

	};

	Common.initialize();
	$(document).ready(_.bind(Common.render, Common));
	$(window).on('load', _.bind(Common.onLoad, Common));
	$(window).on('unload', _.bind(Common.unLoad, Common));
}(
	window,
	window.jQuery,
	window._
));

//TAP=hover
! function() {
	var n = "",
		t = "",
		e = window.Hover = function(n) {
			return new e.fn.init(n)
		};
	e.fn = {
		init: function(n) {
			this.prop = n
		},
		bind: function(o, i) {
			t = o, n = i, $(window).bind("touchstart", function(t) {
				var o = t.target || window.target,
					i = null;
				"A" == o.tagName || $(o).hasClass(n) ? i = $(o) : $(o).parents("a").length > 0 ? i = $(o).parents("a") : $(o).parents("." + n).length > 0 && (i = $(o).parents("." + n)), null != i && e().touchstartHoverElement(i)
			})
		},
		touchstartHoverElement: function(n) {
			n.addClass(t), n.unbind("touchmove", e().touchmoveHoverElement), n.bind("touchmove", e().touchmoveHoverElement), n.unbind("touchend", e().touchendHoverElement), n.bind("touchend", e().touchendHoverElement)
		},
		touchmoveHoverElement: function(n) {
			$(this).removeClass(t)
		},
		touchendHoverElement: function(n) {
			$(this).removeClass(t)
		}
	}, e.fn.init.prototype = e.fn, e().bind("hover", "tap")
}();